﻿using eShop.Core;
using eShop.Services;
using eShop.Web.Admin.Models;
using eShop.Web.Framework;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web.Mvc;

namespace eShop.Web.Admin.controllers
{
    /// <summary>
    /// 客户信息
    /// </summary>
    public partial class CustomerController : BaseAdminController
    {
        public ActionResult List()
        {
            CustomerModel model = new CustomerModel();
            model.CusotmerList = AdminCustomer.AdminGetCustomerList();
            ShopUtils.SetAdminRefererCookie(Url.Action("List"));
            return View(model);
        }


        /// <summary>
        /// 添加页面
        /// </summary>
        [HttpGet]
        public ActionResult Add()
        {
            CustomerModel model = new CustomerModel();
            model.Regionid = 0;
            //model.CusotmerList = AdminCustomer.AdminGetCustomerList();
            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
            return View(model);
        }

        /// <summary>
        /// 添加action
        /// </summary>
        [HttpPost]
        public ActionResult Add(CustomerModel model)
        {
            if (string.IsNullOrWhiteSpace(model.EnCode))
                ModelState.AddModelError("EnCode", "客户编号不能为空");

            if (AdminUsers.IsExistUserName(model.FullName))
                ModelState.AddModelError("FullName", "名称已经存在");

            if (ModelState.IsValid)
            {
                CustomerInfo customerInfo = new CustomerInfo()
                {
                    Encode = model.EnCode,
                    Fullname = model.FullName,
                    Shortname = model.ShortName,
                    Custindustryid = model.CustinDustryid,
                    Custtypeid = model.CustTypeid,
                    Custdegreeid = model.CustDegreeid,
                    Regionid = model.Regionid,
                    Contact = model.Contact,
                    Mobile = model.Mobile,
                    Tel = model.Tel,
                    Fax = model.Fax,
                    Email = model.Email,
                    Companyaddress = model.CompanyAddress,
                    Companysite = model.CompanySite,
                    Traceusername = model.TraceUserName,
                    Description = model.Description
                };
                AdminCustomer.CreateCustomer(customerInfo);
                AddAdminOperateLog("添加客户", "添加客户,客户为:" + model.FullName);
                return PromptView("添加客户成功");
            }
            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
            return View(model);
        }

        /// <summary>
        /// 编辑页面
        /// </summary>
        [HttpGet]
        public ActionResult Edit(int wid = -1)
        {
            CustomerModel model = new CustomerModel();
            DataTable dt = AdminCustomer.AdminGetCustomerList();
            foreach (DataRow dr in dt.Rows)
            {
                if ((int)dr["customerid"] == wid)
                {
                    model.EnCode = dr["encode"].ToString();
                    model.FullName = dr["fullname"].ToString();
                    model.ShortName = dr["shortname"].ToString();

                    model.CustinDustryid = dr["custindustryid"].ToString();
                    model.CustTypeid = dr["custtypeid"].ToString();
                    model.CustLevelid = dr["custlevelid"].ToString();

                    model.Regionid = int.Parse(dr["regionid"].ToString());
                    model.Contact = dr["contact"].ToString();
                    model.Mobile = dr["mobile"].ToString();

                    model.Tel = dr["tel"].ToString();
                    model.Fax = dr["fax"].ToString();
                    model.Email = dr["email"].ToString();

                    model.CompanyAddress = dr["companyaddress"].ToString();
                    model.CompanySite = dr["companysite"].ToString();
                    model.TraceUserName = dr["traceusername"].ToString();
                    model.Description = dr["description"].ToString();

                    break;
                }

            }
            if (string.IsNullOrWhiteSpace(model.EnCode))
                return PromptView("数据不存在");

            //所在地区
            RegionInfo regionInfo = Regions.GetRegionById(model.Regionid);
            if (regionInfo != null)
            {
                ViewData["provinceId"] = regionInfo.ProvinceId;
                ViewData["cityId"] = regionInfo.CityId;
                ViewData["countyId"] = regionInfo.RegionId;
            }
            else
            {
                ViewData["provinceId"] = -1;
                ViewData["cityId"] = -1;
                ViewData["countyId"] = -1;
            }

            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
            return View(model);
        }

        /// <summary>
        /// 编辑action
        /// </summary>
        [HttpPost]
        public ActionResult Edit(CustomerModel model, int wid = -1)
        {
            CustomerInfo info = new CustomerInfo()
            {
                Customerid = wid, //编号 主键
                Encode = model.EnCode,
                Fullname = model.FullName,
                Shortname = model.ShortName,
                Custindustryid = model.CustinDustryid,
                Custtypeid = model.CustTypeid,
                Custdegreeid = model.CustDegreeid,
                Regionid = model.Regionid,
                Contact = model.Contact,
                Mobile = model.Mobile,
                Tel = model.Tel,
                Fax = model.Fax,
                Email = model.Email,
                Companyaddress = model.CompanyAddress,
                Companysite = model.CompanySite,
                Traceusername = model.TraceUserName,
                Description = model.Description
            };
            if (info.Customerid <= 0) return PromptView("客户主键不存在");
            if (string.IsNullOrWhiteSpace(info.Encode)) return PromptView("客户不存在");
            AdminCustomer.UpdateCustomer(info);
            ViewData["referer"] = ShopUtils.GetAdminRefererCookie();
            return PromptView("修改成功");
        }

        /// <summary>
        /// 删除
        /// </summary>
        public ActionResult Del(int wid = -1)
        {
            int result = AdminCustomer.DeleteCustomerByid(wid);
            if (result == 1)
            {
                AddAdminOperateLog("删除客户", "删除客户,客户Id为:" + wid);
                return PromptView("删除成功");
            }
            else
            {
                return PromptView("删除失败，可能已经被使用!");
            }
        }
    }
}
